package lib.bone.flow.dao;

import lib.bone.flow.domain.entity.FlowTask;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

public interface FlowTaskDAO extends JpaRepository<FlowTask,String> {




    /**
     * 根据流程实例id删除
     * @param flowWorkId
     */
    @Modifying
    @Transactional
    void deleteByFlowWorkId(String flowWorkId);

    List<FlowTask> findByServiceId(String id);


    /**
     * 根据ID列表删除
     * @param ids
     */
    @Modifying
    @Transactional
    void deleteByIdIn(List<String> ids);

    /**
     * 根据流程ID 和 节点ID删除
     * @param flowWorkId
     * @param flowNodeId
     */
    @Deprecated
    void deleteByFlowWorkIdAndFlowNodeId(String flowWorkId,String flowNodeId);

    /**
     * 根据流程实例ID 和 流程任务状态查询
     * @param flowWorkId
     * @param taskStatus
     * @return
     */
    List<FlowTask> findByFlowWorkIdAndTaskStatus(String flowWorkId,String taskStatus);
    /**
     * 修改全部符合条件的TASK 状态
     */
    @Modifying
    @Query("update FlowTask task set task.taskStatus = :taskStatus where task.id in (:ids)")
    void updateTaskStatusByIds(String taskStatus, List<String> ids);

    /**
     * 根据 flowWorkId 和 flowNodeId 进行修改
     */
    @Modifying
    @Query("update FlowTask task set task.taskStatus = :taskStatus where task.flowWorkId = :flowWorkId and task.flowNodeId = :flowNodeId")
    void updateTaskStatusByFlowWorkIdAndFlowNodeId(String taskStatus,String flowWorkId,String flowNodeId);

    /**
     * 根据 flowWorkId 和 flowNodeId 查询
     */

    List<FlowTask> findByFlowWorkIdAndFlowNodeId(String flowWorkId,String flowNodeId);




    /**
     * 统计通过的个数
     * 状态为 FINISHED
     */
    Long countByActUserIdAndTaskStatus(String actUserId,String taskStatus);
    /**
     * 根据状态统计
     */
    Long countByTaskStatus(String taskStatus);

    Optional<FlowTask> findByActUserIdAndServiceId(String actUserId,String serviceId);

    Optional<FlowTask> findByFlowWorkId(String flowWorkId);

    List<FlowTask> getByInitiator(String initiator);

    Optional<FlowTask> findAllByServiceIdAndTaskName(String serviceId,String taskName);

//    List<FlowTask> findByServiceIdAndTaskStatus(String serviceId,String taskStatus);
}
